
/*
*
*	The Gimme ECMAScript Library by Stephen Stchur
*	"Taking the pain out of Javascript!"
*
*	Copyright (c) Microsoft Corporation.  All rights reserved.
*
*
*/

Gimme.Ajax = new function()
{
	function requestJson(_uri, _callback)	
	{
		doAsyncRequest(_uri, _callback, true);
	}
	this.request_json = requestJson;
	//[AltSyntax]this.requestJson = requestJson;

	function requestAhah(_uri, _callback)
	{
		doAsyncRequest(_uri, _callback, false);
	}
	this.request_ahah = requestAhah;
	//[AltSyntax]this.requestAhah = requestAhah;

	function createXHR()
	{
		if (typeof XMLHttpRequest !== 'undefined')
		{
			return new XMLHttpRequest();
		}
		else if (typeof ActiveXObject !== 'undefined')
		{
			try { return new ActiveXObject('Microsoft.XMLHTTP'); }
			catch(everything) { throw new Error('Error invoking XMLHTTP'); }
		}
		else
		{
			throw new Error('XMLHttp is not supported');
		}
	}

	// XHR Stuff
	function doAsyncRequest(_uri, _callback, _isJSON)
	{
		var xhr = createXHR();
		xhr.onreadystatechange = function()
		{
			if (xhr.readyState === 4)
			{
	            var data = xhr.responseText;
                if (_isJSON && data)
				{
					data = Gimme.Internals.parseJSON(data);
				}
                _callback.call(xhr, data, xhr.status);
                xhr = null;
                _callback = null;
			}
		};
		xhr.open('GET', _uri, true);
		xhr.send('');
	}
};

